package com.yeyks.app.controller.base.consumer;

import cn.hutool.extra.servlet.ServletUtil;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.ArgumentsCheckUtil;
import com.yeyks.common.utils.TextValidator;
import com.yeyks.commonReference.service.consumer.UserMemberCardService;
import com.yeyks.consumer.dal.domain.MemberCardDetail;
import com.yeyks.consumer.dto.UserMemberCardDTO;
import com.yeyks.consumer.param.UserMemberCard.*;
import com.yeyks.consumer.service.MemberCardDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/05/16 14:08
 */
@Api(tags = "用户会员卡接口", description = "用户会员卡(token放入请求头中，名字为headerToken)")
@Slf4j
@RestController
@RequestMapping(value = "/app/user/member/card")
public class UserMemberCardController {

    @Autowired
    private UserMemberCardService userMemberCardService;
    @Autowired
    private MemberCardDetailService detailService;

    @ApiOperation(value = "获取用户的会员卡信息:v3.5", notes = "获取用户的会员卡信息", response = UserMemberCardDTO.class)
    @PostMapping(value = "/info")
    public AjaxResponseBody<UserMemberCardDTO> info(@LoadUserId UserMemberCardInfoParam param) {
        UserMemberCardDTO userMemberCardDTO = userMemberCardService.getDTOByUserId(param);
        return AjaxResponseBody.success(userMemberCardDTO);
    }

    @ApiOperation(value = "校验用户需要绑定的会员卡信息:v3.5", notes = "根据code判断(成功code为0),(失败code为-1并将错误信息抛出)")
    @PostMapping(value = "/check")
    public AjaxResponseBody checkCard(@LoadUserId UserMemberCardCheckCardParam param) {
        checkParam(param.getCardNo(), param.getPassword());
        userMemberCardService.checkCard(param.getCardNo(), param.getPassword(), param.getUserId(), param.getCardType());
        return AjaxResponseBody.success();
    }


    @ApiOperation(value = "用户绑定会员卡(带支付密码):v3.5", notes = "根据code判断(绑定成功code为0),(绑定失败code为-1并将错误信息抛出)")
    @PostMapping(value = "/all/bind")
    public AjaxResponseBody allBindCard(@LoadUserId UserMemberCardBindCardParam param) {
        checkParam(param.getCardNo(), param.getPassword());
        if (ArgumentsCheckUtil.strIsNull(param.getPayPassword())) {
            throw new ServiceException("支付密码不能为空");
        }
        userMemberCardService.userBindCard(param);
        return AjaxResponseBody.success();
    }

    @ApiOperation(value = "用户忘记支付密码,需要md5（忘记密码，验证手机号后调该接口）:v3.5", notes = "根据code判断(设置成功code为0 msg为success, 设置失败code为-1并将错误信息抛出)")
    @PostMapping(value = "/set/pwd")
    public AjaxResponseBody setPwd(@LoadUserId UserMemberCardSetPwdParam param) {
        userMemberCardService.setPwd(param);
        return AjaxResponseBody.success();
    }

    @ApiOperation(value = "用户修改支付密码，需要md5:v3.5", notes = "根据code判断(修改成功code为0 msg为success, 修改失败code为-1并将错误信息抛出)")
    @PostMapping(value = "/update/pwd")
    public AjaxResponseBody updatePwd(@Validated @LoadUserId UserMemberCardUpdatePwdParam param) {
        userMemberCardService.updatePwd(param);
        return AjaxResponseBody.success();
    }

    @ApiOperation(value = "用户生成充值订单:v3.5", notes = "返回充值订单id(拿id调支付)")
    @PostMapping(value = "/recharge/order")
    public AjaxResponseBody<Map<String, Object>> rechargeOrder(@Validated @LoadUserId UserMemberCardRechargeOrderParam param) {
        Map<String, Object> map = userMemberCardService.rechargeOrder(param);
        return AjaxResponseBody.success(map);
    }

    @ApiOperation(value = "用户会员卡充值支付:v3.5", notes = "type(wxpay, alipay, xcx, gzh)")
    @PostMapping(value = "/recharge/pay")
    public AjaxResponseBody<Object> rechargePay(@Validated @LoadUserId UserMemberCardRechargePayParam param, HttpServletRequest request) {
        param.setIp(ServletUtil.getClientIP(request));
        Object object = userMemberCardService.rechargePay(param);
        return AjaxResponseBody.success(object);
    }

    @ApiOperation(value = "用户获取会员卡余额明细:v3.5", notes = "用户获取会员卡余额明细", response = MemberCardDetail.class)
    @PostMapping(value = "/detail/list")
    public AjaxResponseBody<List<MemberCardDetail>> detailList(@LoadUserId UserMemberCardDetailListParam param) {
        List<MemberCardDetail> memberCardDetails = detailService.list(param);
        return AjaxResponseBody.success(memberCardDetails);
    }

    private void checkParam(String cardNo, String password) {
        if (ArgumentsCheckUtil.strIsNull(cardNo)) {
            throw new ServiceException("卡号不能为空");
        }
        if (!TextValidator.DIGITAL.boolCheck(cardNo)) {
            throw new ServiceException("卡号输入非法");
        }
        if (ArgumentsCheckUtil.strIsNull(password)) {
            throw new ServiceException("密码不能为空");
        }
    }

    @ApiOperation(value = "检查是否设置密码:v4.3.0", notes = "检查是否设置密码")
    @PostMapping(value = "/check/pwd")
    public AjaxResponseBody checkPwd(@LoadUserId UserMemberCardCheckPwdParam param) {
        return AjaxResponseBody.success(userMemberCardService.checkPwd(param.getUserId(), param.getCardType()));
    }

}
